<style lang="less">
.login{
    width: 100%;
    height: 100%;
    background-image: url('../images/loginbd.jpg');
    background-size: cover;
    background-position: center;
    position: relative;
    &-con{
        margin:auto;
        position: absolute;
        left: 50%;
        top: 30%;
        transform: translate(-50%,-50%);
        width: 300px;
        &-header{
            font-size: 16px;
            font-weight: 300;
            text-align: center;
            padding: 30px 0;
        }
        .form-con{
            padding: 10px 0 0;
        }
        .login-tip{
            font-size: 10px;
            text-align: center;
            color: #c3c3c3;
        }
    }
}

</style>

<template>
    <div class="login" keydown.enter="handleSubmit('')">
        <div class="login-con">
        <center><font size="6" color="#fffff0"></font></center>
        <BR/>
            <Card>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="username">
                            <Input v-model="form.username" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                        <Input type="password" v-model="form.password" placeholder="请输入密码">
                            <span slot="prepend">
                                <Icon :size="14" type="locked"></Icon>
                            </span>
                        </Input>
                        </FormItem>
                        <FormItem v-if="debug">
                            <Button @click="handleSubmit('')" type="primary" long>登录</Button>
                        </FormItem>
                        <FormItem v-else>
                            <div class="l-captcha" data-site-key="28b0f1ba9bba5e197b5db736c5875152" data-callback="login"></div> 
                        </FormItem>
                    </Form>
                    <p class="login-tip"></p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import util from '@/libs/util.js';
import CryptoJS from 'crypto-js'

export default {
    data () {
        return {
            form: {
                username: '',
                password: ''
            },
            rules: {
                username: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            },
            debug: document.domain=='127.0.0.1'||document.domain=='',
        };
    },
    mounted() {
        const script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'http://captcha.luosimao.com/static/js/api.js';
        document.body.appendChild(script);
        let self = this;
        window.login = function(verify){
            self.handleSubmit(verify);
        };
    },
    methods: {
        handleSubmit (verify) {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    let data = {
                        'u':this.form.username,
                        'p':CryptoJS.SHA256(this.form.password).toString(),
                        'v':verify||CryptoJS.SHA1(this.form.password).toString(),
                        'username':this.form.username,
                        'password':CryptoJS.SHA512(this.form.password).toString(),
                    };
                    util.ajax({
                        method: 'POST',
                        action: 'userlogin',
                        json:   data
                    }).then(res => {
                        this.projectselect(res.projectid);
                        Cookies.set('user', res.username);
                        Cookies.set('access', res.group);
                        localStorage.token = res.token;
                        this.$store.commit('setAvator','');
                        this.$router.push({
                            name: 'project_index'
                        });
                    }).catch(err => {
                        this.$Message.error(err);
                        window.LUOCAPTCHA.reset();
                    });
                };
            });
        },
        projectselect(projectid){
            this.projectid = projectid;
            util.ajax({
                method:'POST',
                action:'projectselect',
                json:{'projectid':projectid}
            }).then(res => {
                localStorage.projectid = res.projectid;
                localStorage.projectname = res.projectname;
                this.$Message.info('确定选择该系统');
            }).catch(err => {
                this.$Message.error(err);
            });
        },
    }
};
</script>

